package dawi.fcs.servicio.util;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
private static SqlSessionFactory factory;
private static SqlSession currentSession;	
	public MyBatisUtil() {}
	
	static{
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader("mybatis-configuration.xml");
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}
		factory = new SqlSessionFactoryBuilder().build(reader);
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return factory; 
	}
	
	public static SqlSession getCurrentSession(){
		if (currentSession==null)
			currentSession = factory.openSession(true);
		return currentSession;
	}
}
